package com.example.resturant.controller;

import com.example.resturant.model.Address;
import com.example.resturant.repository.AddressRepository;
import com.example.resturant.vo.UserVO;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/cart")
@Controller
public class CartController {

    @Resource
    private AddressRepository addressRepository;

    @GetMapping
    public String cart(Model model, HttpSession httpSession) {
        Boolean logined = (Boolean) httpSession.getAttribute("logined");
        if (BooleanUtils.isNotTrue(logined)) {
            return "redirect:/index";
        }
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        if (userVO == null) {
            return "redirect:/index";
        }
        List<Address> addresses = addressRepository.findByUserIdAndDeleted(userVO.getId(), false);
        model.addAttribute("addresses", addresses);
        return "cart";
    }
}
